package com.nanyang.tms.service.company.impl;

import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.companyadjustment.CompanyAdjustmentAddRequest;
import com.nanyang.tms.api.dto.request.companyadjustment.CompanyAdjustmentQueryRequest;
import com.nanyang.tms.api.dto.response.companyadjustment.CompanyAdjustmentResponse;
import com.nanyang.tms.api.service.companyadjustment.CompanyAdjustmentRpcService;
import com.nanyang.tms.dao.company.CompanyAdjustmentDao;
import com.nanyang.tms.domain.company.CompanyAdjustmentDO;
import com.nanyang.tms.domain.company.CompanyAdjustmentQuery;
import com.nanyang.tms.domain.company.CompanyAdjustmentVO;
import com.nanyang.tms.manager.company.CompanyAdjustmentManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class CompanyAdjustmentRpcServiceImpl implements CompanyAdjustmentRpcService {

    @Autowired
    private CompanyAdjustmentManager companyAdjustmentManager;
    @Autowired
    private CompanyAdjustmentDao companyAdjustmentDao;
    @Override
    public ApiResult<PageBean<CompanyAdjustmentResponse>> listCompanyAdjustment(CompanyAdjustmentQueryRequest param) {
        ApiResult<PageBean<CompanyAdjustmentResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyAdjustmentQuery query = new CompanyAdjustmentQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<CompanyAdjustmentVO> companyAdjustmentList = companyAdjustmentManager.listCompanyAdjustment(query);
            PageBean<CompanyAdjustmentResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(companyAdjustmentList.getTotalItem());
            BeanUtils.copyListProperties(companyAdjustmentList.getData(), resultList.getData(), CompanyAdjustmentResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }


    @Override
    public ApiResult<Boolean> saveCompanyAdjustment(CompanyAdjustmentAddRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyAdjustmentDO companyAdjustmentDo = new  CompanyAdjustmentDO();
            BeanUtils.copyProperties(param,companyAdjustmentDo);
            companyAdjustmentDo.setGmtCreatePin(param.getPin());
            companyAdjustmentManager.saveCompanyAdjustment(companyAdjustmentDo);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

}
